//
// DepComponentFile
//
// Copyright (C) jextra.net.
//
//  This file is part of the net.jextra.dep Library.
//
//  The net.jextra.dep Library is free software; you can redistribute it and/or
//  modify it under the terms of the GNU Lesser General Public
//  License as published by the Free Software Foundation; either
//  version 2.1 of the License, or (at your option) any later version.
//
//  The net.jextra.dep Library is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
//  Lesser General Public License for more details.
//
//  You should have received a copy of the GNU Lesser General Public
//  License along with the net.jextra.dep Library; if not, write to the Free
//  Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
//  02111-1307 USA.
//

package net.jextra.dep;

import java.io.*;

/**
 * <p>
 * Represents an published/exposed file that constitutes this component. Note, a component may consist of multiple files.
 * </p>
 */
public class DepComponentFile
{
    // ============================================================
    // Fields
    // ============================================================

    private DepComponent component;
    private String name;

    // ============================================================
    // Constructors
    // ============================================================

    public DepComponentFile( DepComponent component, String name )
    {
        this.component = component;
        this.name = name;
    }

    // ============================================================
    // Methods
    // ============================================================

    // ----------
    // public
    // ----------

    public DepComponent getComponent()
    {
        return component;
    }

    public void setComponent( DepComponent component )
    {
        this.component = component;
    }

    public String getName()
    {
        return name;
    }

    public void setName( String name )
    {
        this.name = name;
        if ( this.name != null )
        {
            this.name = this.name.trim().replace( '/', '.' ).replace( '\\', '.' );
        }
    }

    public InputStream getInputStream()
        throws Exception
    {
        return component.createInputStream( name );
    }

    public Object getHashKey()
    {
        return name;
    }

    @Override
    public int hashCode()
    {
        return getHashKey() == null ? 0 : getHashKey().hashCode();
    }

    @Override
    public boolean equals( Object other )
    {
        if ( getHashKey() != null && other instanceof DepComponentFile )
        {
            return getHashKey().equals( ( (DepComponentFile) other ).getHashKey() );
        }
        return false;
    }
}
